<template>
  <div class="account-settings-info-view">
    <a-row :gutter="16">
      <a-col :md="24" :lg="16">

        <a-form :form="form" layout="vertical">
          <a-form-item label="公司名称" >
            <a-input v-decorator="['cname', validatorRules.cname]" placeholder="请输入公司名称" />
          </a-form-item>

          <a-form-item label="手机号" >
            <a-input v-decorator="['tel', validatorRules.tel]" placeholder="手机号" />
          </a-form-item>

          <a-form-item label="联系地址" >
            <a-input v-decorator="['address', validatorRules.address]" placeholder="联系地址" />
          </a-form-item>

          <a-form-item label="统一社会信用代码" >
            <a-input v-decorator="['uniscid']" disabled placeholder="统一社会信用代码" />
          </a-form-item>

          <a-form-item>
            <a-button @click="submitForm" type="primary">提交</a-button>
            <!--<a-button style="margin-left: 8px">保存</a-button>-->
          </a-form-item>
        </a-form>

      </a-col>
    </a-row>

    <avatar-modal ref="modal">

    </avatar-modal>
  </div>
</template>

<script>
  import AvatarModal from './AvatarModal'
  import { httpAction, getAction } from '@/api/manage'
  import store from '@/store'

  export default {
    components: {
      AvatarModal
    },
    data () {
      return {
        form: this.$form.createForm(this),
        // cropper
        preview: {},
        validatorRules: {
          cname: {
            rules: [
              { required: true, message: '请输入公司名称!'},
            ]
          },
          tel: {
            rules: [
              { required: true, message: '请输入联系方式手机号!'},
              { pattern: /^1[3456789]\d{9}$/, message: '请输入正确的手机号码!'},
            ]
          },
          address: {
            rules: [
              { required: true, message: '请输入联系地址!'},
            ]
          }
        },
        url: {
          queryByLoginname: "/customer/tbCustomer/queryByLoginname",
          edit: "/customer/tbCustomer/edit",
        }
      }
    },
    created() {
      const that = this;
      that.initialization();
    },
    methods: {
      initialization() {
        const that = this;
        let loginname = store.getters.userInfo.username;
        //loginname = '15640251323';//测试数据
        getAction(this.url.queryByLoginname,{loginname: loginname}).then((res)=>{
          if(res.success){
            that.form.setFieldsValue({
              cname: res.result.cname,
              tel: res.result.tel,
              address: res.result.address,
              uniscid: res.result.uniscid,
            })
          }
        })
      },

      submitForm () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            let httpurl = this.url.edit;
            let method = 'put';
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.error(res.message);
              }
            }).finally(() => {
            })
          }

        })
      }
    }
  }
</script>

<style lang="less" scoped>

  .avatar-upload-wrapper {
    height: 200px;
    width: 100%;
  }

  .ant-upload-preview {
    position: relative;
    margin: 0 auto;
    width: 100%;
    max-width: 180px;
    border-radius: 50%;
    box-shadow: 0 0 4px #ccc;

    .upload-icon {
      position: absolute;
      top: 0;
      right: 10px;
      font-size: 1.4rem;
      padding: 0.5rem;
      background: rgba(222, 221, 221, 0.7);
      border-radius: 50%;
      border: 1px solid rgba(0, 0, 0, 0.2);
    }
    .mask {
      opacity: 0;
      position: absolute;
      background: rgba(0,0,0,0.4);
      cursor: pointer;
      transition: opacity 0.4s;

      &:hover {
        opacity: 1;
      }

      i {
        font-size: 2rem;
        position: absolute;
        top: 50%;
        left: 50%;
        margin-left: -1rem;
        margin-top: -1rem;
        color: #d6d6d6;
      }
    }

    img, .mask {
      width: 100%;
      max-width: 180px;
      height: 100%;
      border-radius: 50%;
      overflow: hidden;
    }
  }
</style>